package com.bsoft.gol.hcs.clinical.dao;


import com.bsoft.gol.hcs.clinical.bean.req.BaseTcmDiseasesRequest;
import com.bsoft.gol.hcs.clinical.bean.req.QueryDiagnosticListRequest;
import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.spring.boot.annotation.SsdevDao;
import ctd.util.annotation.RpcService;
import ctd.util.converter.ConversionUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.StatelessSession;
import org.hibernate.criterion.CriteriaSpecification;
import pcn.diagnosis.BaseTcmDiseases;
import com.bsoft.gol.hcs.specialist.bean.request.QueryOperativeListRequest;
import com.bsoft.gol.hcs.specialist.bean.response.OperativeListResp;
import com.bsoft.gol.hcs.utils.CommonUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@SsdevDao( serviceId = "baseTcmDiseasesDAOService")
public abstract class BaseTcmDiseasesDAO extends HibernateSupportDelegateDAO<BaseTcmDiseases> {
    @DAOMethod(sql = " from BaseTcmDiseases where tcmDiseasesId =:tcmDiseasesId ")
    public abstract BaseTcmDiseases getBaseTcmDiseases(@DAOParam("tcmDiseasesId") int tcmDiseasesId);

    @RpcService
    @DAOMethod(sql = " from BaseTcmDiseases where diseasesCode =:diseasesCode ")
    public abstract BaseTcmDiseases getBaseTcmDiseasesByDiseasesCode(@DAOParam("diseasesCode") String diseasesCode);

    public List<BaseTcmDiseases> queryDiagnosticList(final QueryDiagnosticListRequest request) {
        HibernateStatelessResultAction<List<BaseTcmDiseases>> action = new AbstractHibernateStatelessResultAction<List<BaseTcmDiseases>>() {
            @Override
            public void execute(StatelessSession ss) {
                StringBuffer stringBuffer = getDiagnosticInformationStringBuffer(request);
                SQLQuery sqlQuery = ss.createSQLQuery(stringBuffer.toString());
                int first = request.getPageNo() > 0 ? (request.getPageNo() - 1) * request.getPageSize() : 0;
                sqlQuery.setFirstResult(first);
                sqlQuery.setMaxResults(request.getPageSize());
                sqlQuery.addEntity(BaseTcmDiseases.class);
                setResult(sqlQuery.list());
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    public List<OperativeListResp> queryDiagnosticListByPinYin(final QueryOperativeListRequest request) {
        HibernateStatelessResultAction<List<OperativeListResp>> action = new AbstractHibernateStatelessResultAction<List<OperativeListResp>>() {
            @Override
            public void execute(StatelessSession ss) {
                StringBuffer stringBuffer = getDiagnosticInformationStringByPinYin(request);
                SQLQuery sqlQuery = ss.createSQLQuery(stringBuffer.toString());
                int first = request.getPageNo() > 0 ? (request.getPageNo() - 1) * request.getPageSize() : 0;
                sqlQuery.setFirstResult(first);
                sqlQuery.setMaxResults(request.getPageSize());
                sqlQuery.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                List<Map> dbList = sqlQuery.list();
                List<OperativeListResp> listResps = new ArrayList<>();
                for (Map map : dbList) {
                    listResps.add(ConversionUtils.convert(map, OperativeListResp.class));
                }
                setResult(listResps);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    public BigInteger queryDiagnosticListTotal(final QueryDiagnosticListRequest request) {
        HibernateStatelessResultAction<BigInteger> action = new AbstractHibernateStatelessResultAction<BigInteger>() {
            @Override
            public void execute(StatelessSession ss) {
                StringBuffer stringBuffer = new StringBuffer();
                stringBuffer.append("select COUNT(0) from ( " + getDiagnosticInformationStringBuffer(request) + " ) t");
                Query sqlQuery = ss.createSQLQuery(stringBuffer.toString());
                List list = sqlQuery.list();
                if (list.size() == 1) {
                    Object o = list.get(0);
                    BigInteger count = (BigInteger) o;
                    setResult(count);
                }
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    private StringBuffer getDiagnosticInformationStringBuffer(QueryDiagnosticListRequest request) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(" select tcm_diseases_id, classify_id, diseases_code, diseases_name, pinyin_code, sex_restriction_flag, " +
                "cure_flag, improve_flag, no_healing_flag, death_flag, other_flag, control_mode, delete_flag, delete_dt, " +
                "delete_user, wubi_code, other_code, remark from base_tcm_diseases WHERE  delete_flag = '0'  ");
        if (StringUtils.isNotEmpty(request.getKeyWord())) {
            stringBuffer.append(" and pinyin_code like '" + request.getKeyWord() + "%' OR diseases_name like '" + request.getKeyWord() + "%'");
        }
        return stringBuffer;
    }


    private StringBuffer getDiagnosticInformationStringByPinYin(QueryOperativeListRequest request) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(" select diseases_code relateRecordId, diseases_name   relateName  from base_tcm_diseases WHERE  delete_flag = '0'  ");
        if (StringUtils.isNotEmpty(request.getPyCode())) {
            stringBuffer.append(" and pinyin_code like '" + request.getPyCode().toUpperCase() + "%' ");
        }
        return stringBuffer;
    }

    private StringBuffer getBaseTcmDiseasesStringBuffer(BaseTcmDiseasesRequest request) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(" select tcm_diseases_id, classify_id, diseases_code, diseases_name, pinyin_code, sex_restriction_flag, " +
                "cure_flag, improve_flag, no_healing_flag, death_flag, other_flag, control_mode, delete_flag, delete_dt, " +
                "delete_user, wubi_code, other_code, remark from base_tcm_diseases WHERE  delete_flag = '0'  ");
        if (StringUtils.isNotEmpty(request.getPinyinCode())) {
            stringBuffer.append(" and pinyin_code like '" + request.getPinyinCode() + "%' ");
        }
        if (CommonUtils.isNotEmpty(request.getDiseasesName())) {
            stringBuffer.append(" and diseases_name like '" + request.getDiseasesName() + "%' ");
        }
        if (CommonUtils.isNotEmpty(request.getDiseasesCode())) {
            stringBuffer.append(" and diseases_code like '" + request.getDiseasesCode() + "%' ");
        }
        return stringBuffer;
    }

    public List<BaseTcmDiseases> queryBaseTcmDiseases(final BaseTcmDiseasesRequest request) {
        HibernateStatelessResultAction<List<BaseTcmDiseases>> action = new AbstractHibernateStatelessResultAction<List<BaseTcmDiseases>>() {
            @Override
            public void execute(StatelessSession ss) {
                StringBuffer stringBuffer = getBaseTcmDiseasesStringBuffer(request);
                SQLQuery sqlQuery = ss.createSQLQuery(stringBuffer.toString());
                int first = request.getPageNo() > 0 ? (request.getPageNo() - 1) * request.getPageSize() : 0;
                sqlQuery.setFirstResult(first);
                sqlQuery.setMaxResults(request.getPageSize());
                sqlQuery.addEntity(BaseTcmDiseases.class);
                setResult(sqlQuery.list());
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    public List<BaseTcmDiseases> findBaseTcmDiseases(final BaseTcmDiseasesRequest request) {
        HibernateStatelessResultAction<List<BaseTcmDiseases>> action = new AbstractHibernateStatelessResultAction<List<BaseTcmDiseases>>() {
            @Override
            public void execute(StatelessSession ss) {
                StringBuffer stringBuffer = getBaseTcmDiseasesStringBuffer(request);
                SQLQuery sqlQuery = ss.createSQLQuery(stringBuffer.toString());
                int first = request.getPageNo() > 0 ? (request.getPageNo() - 1) * request.getPageSize() : 0;
                sqlQuery.setFirstResult(first);
                sqlQuery.setMaxResults(request.getPageSize());
                sqlQuery.addEntity(BaseTcmDiseases.class);
                setResult(sqlQuery.list());
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }


    public BigInteger queryDiagnosticListTotal(final BaseTcmDiseasesRequest request) {
        HibernateStatelessResultAction<BigInteger> action = new AbstractHibernateStatelessResultAction<BigInteger>() {
            @Override
            public void execute(StatelessSession ss) {
                StringBuffer stringBuffer = new StringBuffer();
                stringBuffer.append("select COUNT(0) from ( " + getBaseTcmDiseasesStringBuffer(request) + " ) t");
                Query sqlQuery = ss.createSQLQuery(stringBuffer.toString());
                List list = sqlQuery.list();
                if (list.size() == 1) {
                    Object o = list.get(0);
                    BigInteger count = (BigInteger) o;
                    setResult(count);
                }
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    @DAOMethod(sql = " from BaseTcmDiseases order by tcm_diseases_id desc limit 1 ")
    public abstract BaseTcmDiseases getMaxBaseTcmDiseases();
}
